class UserModel {
  final int? uid;
  final String? utoken;
  final String? email;
  final String? realName;
  final String? sex;
  final String? univsId;
  final String? univsName;
  final String? groupType;
  final String? gid;
  final String? isCheckTel;
  final String? authType;
  final String? avatar;
  String? nickName;
  final String? roleId;
  final String? eduToken;
  final String? signature;
  final int? isBind;
  final String? chooseToken;
  final String? saikrApiToken;
  final int? myVipLevel;
  final int? myVipExpired;
  final int? myVipEndTime;
  final String? myVipEndTimeStr;
  final int? nowTime;

  // 构造函数使用可选参数，所有字段均可为空
  UserModel({
    this.uid,
    this.utoken,
    this.email,
    this.realName,
    this.sex,
    this.univsId,
    this.univsName,
    this.groupType,
    this.gid,
    this.isCheckTel,
    this.authType,
    this.avatar,
    this.nickName,
    this.roleId,
    this.eduToken,
    this.signature,
    this.isBind,
    this.chooseToken,
    this.saikrApiToken,
    this.myVipLevel,
    this.myVipExpired,
    this.myVipEndTime,
    this.myVipEndTimeStr,
    this.nowTime,
  });

  /// 从JSON构建模型，处理可能为null的字段
  factory UserModel.fromJson(Map<String, dynamic>? json) {
    // 处理json本身为null的情况
    if (json == null) {
      return UserModel();
    }

    return UserModel(
      uid: int.tryParse(json['uid'].toString()) ?? 0,
      utoken: json['utoken'] as String?,
      email: json['email'] as String?,
      realName: json['real_name'] as String?,
      sex: json['sex'] as String?,
      univsId: json['univs_id'] as String?,
      univsName: json['univs_name'] as String?,
      groupType: json['group_type'] as String?,
      gid: json['gid'] as String?,
      isCheckTel: json['is_check_tel'] as String?,
      authType: json['auth_type'] as String?,
      avatar: json['avatar'] as String?,
      nickName: json['nick_name'] as String?,
      roleId: json['role_id'] as String?,
      eduToken: json['edu_token'] as String?,
      signature: json['signature'] as String?,
      isBind: json['is_bind'] as int?,
      chooseToken: json['choose_token'] as String?,
      saikrApiToken: json['saikr_api_token'] as String?,
      myVipLevel: json['my_vip_level'] as int?,
      myVipExpired: json['my_vip_expired'] as int?,
      myVipEndTime: json['my_vip_end_time'] as int?,
      myVipEndTimeStr: json['my_vip_end_time_str'] as String?,
      nowTime: json['now_time'] as int?,
    );
  }

  /// 模型转JSON，自动忽略null值
  Map<String, dynamic> toJson() {
    return {
      'uid': uid,
      'utoken': utoken,
      'email': email,
      'real_name': realName,
      'sex': sex,
      'univs_id': univsId,
      'univs_name': univsName,
      'group_type': groupType,
      'gid': gid,
      'is_check_tel': isCheckTel,
      'auth_type': authType,
      'avatar': avatar,
      'nick_name': nickName,
      'role_id': roleId,
      'edu_token': eduToken,
      'signature': signature,
      'is_bind': isBind,
      'choose_token': chooseToken,
      'saikr_api_token': saikrApiToken,
      'my_vip_level': myVipLevel,
      'my_vip_expired': myVipExpired,
      'my_vip_end_time': myVipEndTime,
      'my_vip_end_time_str': myVipEndTimeStr,
      'now_time': nowTime,
    }; // 移除值为null的字段
  }
}
